home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / sun / NET / WWW / MeteredStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  1.8 KB  |  111 lines

  1. package sun.net.www;
  2.  
  3. import java.io.FilterInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.URL;
  7. import sun.net.ProgressData;
  8.  
  9. public class MeteredStream extends FilterInputStream {
  10.    private static int total_need;
  11.    private static int total_read;
  12.    private static int total_connections;
  13.    int expected;
  14.    int count;
  15.    public URL url;
  16.    boolean closed;
  17.  
  18.    private static synchronized void globalJustRead(int var0) {
  19.       total_read += var0;
  20.    }
  21.  
  22.    private static synchronized void updateExpected(int var0) {
  23.       total_need += var0;
  24.    }
  25.  
  26.    private static synchronized void addConnection(MeteredStream var0) {
  27.       ++total_connections;
  28.    }
  29.  
  30.    private static synchronized void removeConnection(MeteredStream var0) {
  31.       --total_connections;
  32.       total_read -= var0.expected;
  33.       total_need -= var0.expected;
  34.    }
  35.  
  36.    public static synchronized ProgressReport checkProgress(ProgressReport var0) {
  37.       return var0.set(total_read, total_need, total_connections);
  38.    }
  39.  
  40.    public MeteredStream(InputStream var1, int var2, URL var3) {
  41.       this(var1, var2);
  42.       this.url = var3;
  43.    }
  44.  
  45.    public MeteredStream(InputStream var1, int var2) {
  46.       super(var1);
  47.       this.closed = false;
  48.       updateExpected(var2);
  49.       this.expected = var2;
  50.       addConnection(this);
  51.    }
  52.  
  53.    private final void justRead(int var1) {
  54.       if (this.count + var1 > this.expected) {
  55.          var1 = this.expected - this.count;
  56.       }
  57.  
  58.       this.count += var1;
  59.       globalJustRead(var1);
  60.       ProgressData.pdata.update(this.url, this.count, this.expected);
  61.    }
  62.  
  63.    public int read() throws IOException {
  64.       int var1 = super.read();
  65.       if (var1 != -1) {
  66.          this.justRead(1);
  67.       }
  68.  
  69.       return var1;
  70.    }
  71.  
  72.    public int read(byte[] var1, int var2, int var3) throws IOException {
  73.       int var4 = super.read(var1, var2, var3);
  74.       if (var4 != -1) {
  75.          this.justRead(var4);
  76.       }
  77.  
  78.       return var4;
  79.    }
  80.  
  81.    public long skip(long var1) throws IOException {
  82.       var1 = super.skip(var1);
  83.       if (var1 != -1L) {
  84.          this.justRead((int)var1);
  85.       }
  86.  
  87.       return var1;
  88.    }
  89.  
  90.    public void close() throws IOException {
  91.       super.close();
  92.       if (!this.closed) {
  93.          this.closed = true;
  94.          this.justRead(this.expected - this.count);
  95.          removeConnection(this);
  96.       }
  97.  
  98.       ProgressData.pdata.unregister(this.url);
  99.    }
  100.  
  101.    protected void finalize() {
  102.       if (!this.closed) {
  103.          this.closed = true;
  104.          this.justRead(this.expected - this.count);
  105.          removeConnection(this);
  106.       }
  107.  
  108.       ProgressData.pdata.unregister(this.url);
  109.    }
  110. }
  111.